﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using IssueVision.EntityModel;

namespace IssueVision.Common
{
    public partial class ReportPrintout : UserControl
    {
        public int NumItemsShown { get; private set; }

        public ReportPrintout(WriteableBitmap wb)
        {
            InitializeComponent();

            // set the charts bitmap
            image_Charts.Source = wb;
        }

        public void Paginate(IEnumerable<Issue> issuesToPrint, int startIndex, bool isFirstPage, Size pageSize)
        {
            if (!isFirstPage)
            {
                rowDefinition_Charts.Height = new GridLength(0);
                grid_Charts.Visibility = Visibility.Collapsed;
            }
            stackPanel_Issues.Children.Clear();
            UpdateLayout();
            foreach (var issue in issuesToPrint.Skip(startIndex))
            {
                stackPanel_Issues.Children.Add(new ContentPresenter { Content = issue, ContentTemplate = issueTemplate, HorizontalAlignment = HorizontalAlignment.Stretch });
                UpdateLayout();
                if (DesiredSize.Height >= pageSize.Height)
                {
                    stackPanel_Issues.Children.RemoveAt(stackPanel_Issues.Children.Count - 1);
                    break;
                }
            }
            UpdateLayout();
            NumItemsShown = stackPanel_Issues.Children.Count;
            textBlock_TitleText.Text = string.Format("Issue Report (Issues {0} through {1})", startIndex + 1, startIndex + NumItemsShown);
        }
    }
}
